package egInterface;

import java.security.PublicKey;
import java.util.Arrays;
import java.util.Map;

/*class Person implements Comparable<Person>{
    public String name;
    public int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    //比较规则
    @Override
    public int compareTo(Person o) {
        *//*if(this.age > o.age){
            return 1;
        }else if(this.age == o.age){
            return 0;
        }else{
            return -1;
        }*//*
        //return this.age-o.age;
        return this.name.compareTo(o.name);
    }
}*/
class Money implements Cloneable{
    public double menoy=9.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable{
    public String name;
    public int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public Money m = new Money();
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person p=(Person)super.clone();
        p.m=(Money)this.m.clone();
        return p;
    }
}

public class TestDemo {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person1=new Person("张三",18);
        Person person2= (Person) person1.clone();//此时向下转型，不安全，需要强制类型转换
        //System.out.println(person2);
        System.out.println(person1.m.menoy);//9.9
        System.out.println(person2.m.menoy);//9.9
        System.out.println("================");
        person2.m.menoy=99.9;
        System.out.println(person1.m.menoy);//9.9
        System.out.println(person2.m.menoy);//99.9

    }
    public static void main3(String[] args) {
        Person person1=new Person("张三",18);
        Person person2=new Person("李四",10);
        Person person3=new Person("王五",19);
        AgeComparator ageComparator=new AgeComparator();
        System.out.println(ageComparator.compare(person1, person2));

        NameComparator nameComparator=new NameComparator();
        System.out.println(nameComparator.compare(person1, person2));

        Person[] people={person1,person2,person3};

        Arrays.sort(people,ageComparator);
        System.out.println(Arrays.toString(people));
    }
    public static void main2(String[] args) {
        Person person1=new Person("张三",18);
        Person person2=new Person("李四",10);
        Person person3=new Person("王五",19);
        Person[] people={person1,person2,person3};
        Arrays.sort(people);
        System.out.println(Arrays.toString(people));
    }
    public static void main1(String[] args) {
        Person person1=new Person("张三",18);
        Person person2=new Person("李四",10);
        Person person3=new Person("王五",19);
        //System.out.println(person1.compareTo(person2));
    }
}
